package com.hmall.gateWay.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1、获取请求
        ServerHttpRequest request = exchange.getRequest();
        //2、处理过滤逻辑
        System.out.println("MyGlobalFilter#filter pre 被执行了。。。。" + request.getPath());
        //3、放行
        return chain.filter(exchange).then(Mono.fromRunnable(new Runnable() {
            @Override
            public void run() {//在微服务的请求方法执行完后执行的
                System.out.println("MyGlobalFilter#filter post 被执行了。。。。");
            }
        }));
    }

    @Override
    public int getOrder() {
        //过滤器执行顺序，值越小越优先执行
        return 0;
    }
}
